<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Doctor;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class DoctorController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(Doctor::indexQuery(), function (Grid $grid) {
            $grid->column('thumb')->image('', 100, 100);
            $grid->column('name', '医生信息')->display(function ($value) {

                return collect([
                    '医生姓名' => $value,
                    '医生头衔' => $this->title,
                    '医院名称' => $this->hospital,
                ])->map(function ($value, $key) {
                    return "$key : $value";
                })->join('<br/>');
            });
            $grid->column('info')->width(200)->limit();
            $grid->column('description')->width(400)->limit();
            $grid->column('enable', '显示/隐藏')->switch();
            $grid->column('show_home', '首页显示')->switch();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });

            $grid->sortable();
            $grid->scrollbarX();
            $grid->paginate(100);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Doctor(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('thumb')->image('', 50, 50);
            $show->field('hospital');
            $show->field('title');
            $show->field('info');
            $show->field('description');
            $show->field('enable');
            $show->field('order_n');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Doctor(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('title');
            $form->text('hospital');
            $form->image('thumb')
                ->removable(false)
                ->autoUpload();
            $form->textarea('info');
            $form->textarea('description');
            $form->switch('enable')->default(true);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
